package com.huibo;

import com.huibo.entity.Cache;
import com.huibo.entity.Employee;
import com.huibo.entity.Preference;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest
public class Xmztest {

    @Test
    void functionV01() {
        List<Employee> employeeList = new ArrayList<>();
//        employeeList.add(new Employee(1L, "李1", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(2L, "李2", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(3L, "李3", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(4L, "李4", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(5L, "李5", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(6L, "李6", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(7L, "李7", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(8L, "李8", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(9L, "李9", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(10L, "李10", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(11L, "李11", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(12L, "李12", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(13L, "李13", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(14L, "李14", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(15L, "李15", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(16L, "李16", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(17L, "李17", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(18L, "李18", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(19L, "李19", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));
//        employeeList.add(new Employee(20L, "李20", "123456", "6789@163.com", "15636563698", 1L, 1, 1, 1L));

        List<Cache> cacheList = new ArrayList<>();
        //在第一遍轮询后如果符合时间要求将其加入到缓存队列中，然后再通过工作时长来选择最优的人员
        cacheList.add(new Cache(1L, 1L, 0, 0, 0));
        cacheList.add(new Cache(2L, 2L, 0, 0, 0));
        cacheList.add(new Cache(3L, 3L, 0, 0, 0));
        cacheList.add(new Cache(4L, 4L, 0, 0, 0));
        cacheList.add(new Cache(5L, 5L, 0, 0, 0));
        cacheList.add(new Cache(6L, 6L, 0, 0, 0));
        cacheList.add(new Cache(7L, 7L, 0, 0, 0));
        cacheList.add(new Cache(8L, 8L, 0, 0, 0));
        cacheList.add(new Cache(9L, 9L, 0, 0, 0));
        cacheList.add(new Cache(10L, 10L, 0, 0, 0));
        cacheList.add(new Cache(11L, 11L, 0, 0, 0));
        cacheList.add(new Cache(12L, 12L, 0, 0, 0));
        cacheList.add(new Cache(13L, 13L, 0, 0, 0));
        cacheList.add(new Cache(14L, 14L, 0, 0, 0));
        cacheList.add(new Cache(15L, 15L, 0, 0, 0));
        cacheList.add(new Cache(16L, 16L, 0, 0, 0));
        cacheList.add(new Cache(17L, 17L, 0, 0, 0));
        cacheList.add(new Cache(18L, 18L, 0, 0, 0));
        cacheList.add(new Cache(19L, 19L, 0, 0, 0));
        cacheList.add(new Cache(20L, 20L, 0, 0, 0));

        ArrayList<Preference> preferenceList = new ArrayList<>();
        preferenceList.add(new Preference(1L, 1L, 3, 41, 25, 2, 48, 28, 9));
        preferenceList.add(new Preference(2L, 2L, 26, 5, 4, 18, 27, 40, 13));
        preferenceList.add(new Preference(3L, 3L, 1, 2, 3, 17, 5, 8, 7));
        preferenceList.add(new Preference(4L, 4L, 5, 9, 16, 20, 9, 12, 9));
        preferenceList.add(new Preference(5L, 5L, 40, 54, 13, 12, 33, 16, 33));
        preferenceList.add(new Preference(6L, 6L, 13, 53, 51, 7, 12, 56, 60));
        preferenceList.add(new Preference(7L, 7L, 25, 43, 35, 11, 13, 15, 13));
        preferenceList.add(new Preference(8L, 8L, 24, 18, 50, 19, 6, 13, 30));
        preferenceList.add(new Preference(9L, 9L, 34, 35, 9, 15, 10, 38, 50));
        preferenceList.add(new Preference(10L, 10L, 17, 32, 30, 30, 5, 50, 32));
        preferenceList.add(new Preference(11L, 11L, 51, 7, 27, 56, 50, 56, 53));
        preferenceList.add(new Preference(12L, 12L, 60, 34, 19, 27, 18, 53, 34));
        preferenceList.add(new Preference(13L, 13L, 45, 15, 30, 51, 56, 30, 11));
        preferenceList.add(new Preference(14L, 14L, 42, 54, 15, 38, 54, 8, 19));
        preferenceList.add(new Preference(15L, 15L, 13, 46, 17, 24, 60, 38, 10));
        preferenceList.add(new Preference(16L, 16L, 60, 8, 30, 32, 33, 29, 50));
        preferenceList.add(new Preference(17L, 17L, 36, 38, 8, 4, 27, 24, 24));
        preferenceList.add(new Preference(18L, 18L, 12, 51, 15, 8, 51, 33, 30));
        preferenceList.add(new Preference(19L, 19L, 50, 12, 53, 12, 53, 1, 12));
        preferenceList.add(new Preference(20L, 20L, 12, 8, 60, 10, 29, 2, 9));
        //System.out.println(employeeList.toString());


        //每个员工从周一到周天的有空情况
        int arr[][][]=new int[20][7][6];
        //一周中每个时间段需要的人数
        int[][] employeeNum = new int[7][6];
        employeeNum = new int[][]{{1, 3, 5, 4, 2, 1}, {1, 3, 5, 4, 2, 1}, {1, 3, 5, 4, 2, 1}, {1, 3, 5, 4, 2, 1}, {1, 3, 5, 4, 2, 1}, {1, 3, 5, 4, 2, 1}, {1, 3, 5, 4, 2, 1}};

        for (int i = 0; i < 20; i++) {
            arr[i]=getFreeTime(arr[i], preferenceList.get(i));
        }
//        for (int k = 0; k <20 ; k++) {
//            for (int i = 0; i < 7; i++) {
//                for (int j = 0; j < 6; j++) {
//                    System.out.print(arr[k][i][j] + " ");
//                }
//                System.out.println();
//            }
//            System.out.println();
//        }


        //最终选定的员工表
//        int table[][] = new int[7][6];
        String[][] table = new String[7][6];

        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 6; j++) {
                int num = employeeNum[i][j];
                StringBuilder s = new StringBuilder();
                for (int k = 0; k < 20; k++) {
                    if (arr[k][i][j] == 1) {
                        Cache cache = cacheList.get(k);
                        //如果员工有空，且日连续工作时长和日周时长满足，那么安排这个员工工作
                        //单独判断日连续时长，并清0,
                        if(cache.getConstance()>=4){
//                            employeeList.get(k).setWorkStatus(1);
                            cache.setConstance(0);
                            break;
                        }
                        //判断日周工作时长
                        if( cache.getDayWork()>=8 || cache.getWeekWork()>=40){
                            break;
                        }
                        if(employeeList.get(k).getWorkStatus() == 1 ){
                            //更新他的缓存表
                            //将员工id传入到展示表中,k+1为实际员工id
                            //
//                            table[i][j] +=String.valueOf(k+1)+" ";
                            cache.setConstance(cache.getConstance()+2);
                            cache.setDayWork(cache.getDayWork()+2);
                            cache.setWeekWork(cache.getWeekWork()+2);

//                            Employee employee = employeeList.get(k);
//                            employee.setWorkStatus(0);

                            //安排之后直接跳出遍历员工的循环,人数要求注意
                            if(num!=0) {
                                num--;
//                                table[i][j] +=String.valueOf(k+1)+"_";
                                s.append(String.valueOf(k+1)).append("_");
                            }else{
                                break;
                            }
                        }
                    }
                }
                while(num!=0){
                    //遍历结束都没有选中人，table中默认空缺为String-1，后续处理(根据table和固定规则来强制安排)
//                    table[i][j]+="-1_";
                    s.append("-1_");
                    num--;
                }
                table[i][j] = s.toString();
            }
            //日每天遍历结束后，清除日工作时长
            for(int c = 0;c<cacheList.size();c++){
                cacheList.get(c).setDayWork(0);
                cacheList.get(c).setConstance(0);
            }
        }

        for(int i = 0;i<7;i++){
            for(int j = 0; j<6;j++){
                System.out.print(table[i][j] + "  ");
            }
            System.out.println();
        }



    }

    /**
     * 给一个数返回其代表二进制数组，即判断员工在哪个时间段有空
     *
     * @param num
     * @return
     */
    public int[] change(int num) {
        int arr[] = new int[6];
        int j = 5;
        //System.out.println(Integer.toBinaryString(num));
        for (int i = 5; i >= 0; i--) {
            arr[j--] = (num & (1 << i)) >> i;
        }
        return arr;
    }

    /**
     * 将员工的每周空闲时间放入数组中
     * @param arr
     * @param preference
     * @return
     */
    public int[][] getFreeTime(int arr[][],Preference preference){
        arr[0] = change(preference.getMonday());
        arr[1] = change(preference.getTuesday());
        arr[2] = change(preference.getWednesday());
        arr[3] = change(preference.getThursday());
        arr[4] = change(preference.getFriday());
        arr[5] = change(preference.getSaturday());
        arr[6] = change(preference.getSunday());
        return arr;
    }
}
